﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Entity;
using eBlog.Core;
using eBlog.Service;
namespace eBlog.Web
{
    public partial class Index : PageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            Init_Template();
            TemplateUrl = "index.html";
            LoadDefault();

            int pageIndex;
            if (!int.TryParse(Request.Params["page"], out pageIndex)) { pageIndex = 1; }

             PageSize = 8;

            var catagoryid = Request.QueryString["catagory"];
            var tagsid = Request.QueryString["tags"];
            var searchword = PageValidate.Form("search_query");
            if (!string.IsNullOrEmpty(catagoryid))
            {
                dict.Put(TagFields.POSTS, PostsService.GetPostsList(pageIndex, PageSize, int.Parse(catagoryid)));
                dict.Add("pagenav", Pager.PageInfo(PostsService.GetPostsCount(int.Parse(catagoryid)), PageSize, pageIndex));
            }
            else if(!string.IsNullOrEmpty(searchword))
            {
                dict.Put(TagFields.POSTS, PostsService.FindPostsByKeyWords(searchword));
            }
            else if (!string.IsNullOrEmpty(tagsid))
            {
                dict.Put(TagFields.POSTS, PostsService.GetPostsListByTags(pageIndex, PageSize, int.Parse(tagsid)));
                dict.Add("pagenav", Pager.PageInfo(PostsService.GetPostsCountByTags(int.Parse(tagsid)), PageSize, pageIndex));
            }
            else
            {
                dict.Put(TagFields.POSTS, PostsService.GetPostsList(pageIndex, PageSize));
                dict.Add("pagenav", Pager.PageInfo(PostsService.GetPostsCount(), PageSize, pageIndex));
            }

           
           
            this.WriteHTML();


           
            
        }


        /// <summary>
        /// 加载通用标签
        /// </summary>
        protected void LoadDefault()
        {

//            #region 全局

//            dict.Put(TagFields.SITE_NAME, SettingService.GetSetting().SiteName);
//            dict.Put(TagFields.SITE_DESCRIPTION, SettingService.GetSetting().SiteDescription);
//            dict.Put(TagFields.META_KEYWORDS, SettingService.GetSetting().MetaKeywords);
//            dict.Put(TagFields.META_DESCRIPTION, SettingService.GetSetting().MetaDescription);

//            dict.Put(TagFields.FOOTER_HTML, SettingService.GetSetting().FooterHtml);

//            dict.Put(TagFields.VERSION, SettingService.GetSetting().Version);

//            dict.Put(TagFields.PAGE_TITLE, "首页");

//            dict.Put(TagFields.SITE_PATH, ConfigHelper.SitePath);
//            dict.Put(TagFields.SITE_URL, ConfigHelper.SiteUrl);

//#pragma warning disable 612,618
//            dict.Put(TagFields.THEME_PATH, ConfigHelper.SitePath + "themes/" + themeName + "/");
//#pragma warning restore 612,618
//            dict.Put(TagFields.THEME_URL, ConfigHelper.SiteUrl + "themes/" + themeName + "/");

//            dict.Put(TagFields.IS_DEFAULT, "0");
//            dict.Put(TagFields.IS_POST, "0");

//            //dict.Put(TagFields.FEED_URL, ConfigHelper.SiteUrl + "feed/post" + SettingService.GetSetting().RewriteExtension);
//            //dict.Put(TagFields.FEED_COMMENT_URL, ConfigHelper.SiteUrl + "feed/comment" + SettingService.GetSetting().RewriteExtension);

//            //dict.Put(TagFields.PAGER, string.Empty);
//            //dict.Put(TagFields.PAGER_INDEX, RequestHelper.QueryInt("page", 1));

//            //dict.Put(TagFields.URL, CurrentUrl);
//            dict.Put(TagFields.DATE, DateTime.Now);

//            // dict.Put(TagFields.ARCHIVES, ArchiveManager.GetArchive());

//            dict.Put(TagFields.SEARCH_KEYWORD, string.Empty);

//            dict.Put(TagFields.QUERY_COUNT, 0);
//            dict.Put(TagFields.PROCESS_TIME, 0);

//            dict.Put(TagFields.ENABLE_VERIFYCODE, SettingService.GetSetting().EnableVerifyCode);

//            string headhtml = string.Empty;

//            headhtml += string.Format("<meta name=\"generator\" content=\"Loachs {0}\" />\n", SettingService.GetSetting().Version);
//            headhtml += "<meta name=\"author\" content=\"Loachs Team\" />\n";
//            headhtml += string.Format("<meta name=\"copyright\" content=\"2008-{0} Loachs Team.\" />\n", DateTime.Now.Year);
//            headhtml += string.Format("<link rel=\"alternate\" type=\"application/rss+xml\" title=\"{0}\"  href=\"{1}\"  />\n", SettingService.GetSetting().SiteName, ConfigHelper.SiteUrl + "feed/post" + SettingService.GetSetting().RewriteExtension);
//            headhtml += string.Format("<link rel=\"EditURI\" type=\"application/rsd+xml\" title=\"RSD\" href=\"{0}xmlrpc/rsd.aspx\" />\n", ConfigHelper.SiteUrl);
//            headhtml += string.Format("<link rel=\"wlwmanifest\" type=\"application/wlwmanifest+xml\" href=\"{0}xmlrpc/wlwmanifest.aspx\" />", ConfigHelper.SiteUrl);

//            dict.Put(TagFields.HEAD, headhtml);

//            #endregion

            #region 文章

            //dict.Put(TagFields.POST, null);
            //dict.Put(TagFields.POST_MESSAGE, null);
            //dict.Put(TagFields.POSTS, null);
            dict.Put(TagFields.RECENT_POSTS, PostsService.GetPostsList(1, 10));
            dict.Put(TagFields.RECOMMEND_POSTS, PostsService.GetPostsList(1, 10));
            dict.Put(TagFields.TOP_POSTS, PostsService.GetTopPostList(1, 10));

            //dict.Put(TagFields.FEED_POSTS, null);

            #endregion

            #region 评论

            //dict.Put(TagFields.COMMENTS, null);

            //dict.Put(TagFields.RECENT_COMMENTS, CommentService.GetCommentListByRecent(SettingService.GetSetting().SidebarCommentCount));

            //if (PageUtils.IsLogin)
            //{
            //    UserInfo user = UserManager.GetUser(PageUtils.CurrentUserId);
            //    if (user != null)
            //    {
            //        dict.Put(TagFields.COMMENT_AUTHOR, user.Name);
            //        dict.Put(TagFields.COMMENT_EMAIL, user.Email);
            //        dict.Put(TagFields.COMMENT_SITEURL, user.SiteUrl);
            //    }
            //}
            //else
            //{
            //    dict.Put(TagFields.COMMENT_AUTHOR, Server.UrlDecode(PageUtils.GetCookie(TagFields.COMMENT_AUTHOR)));
            //    dict.Put(TagFields.COMMENT_EMAIL, Server.UrlDecode(PageUtils.GetCookie(TagFields.COMMENT_EMAIL)));
            //    dict.Put(TagFields.COMMENT_SITEURL, Server.UrlDecode(PageUtils.GetCookie(TagFields.COMMENT_SITEURL)));
            //}
            dict.Put(TagFields.COMMENT_CONTENT, string.Empty);
            dict.Put(TagFields.COMMENT_MESSAGE, string.Empty);

            #endregion

            #region 作者,分类,标签

            //dict.Put(TagFields.AUTHORS, UserManager.GetUserList().FindAll(delegate(UserInfo user) { return user.Status == 1; }));
            //dict.Put(TagFields.CATEGORIES, CategoryManager.GetCategoryList());
            //dict.Put(TagFields.RECENT_TAGS, TagManager.GetTagList(SettingService.GetSetting().SidebarTagCount));

            #endregion

            #region 连接

            //dict.Put(TagFields.LINKS, LinkManager.GetLinkList(-1, 1));
            //dict.Put(TagFields.NAV_LINKS, LinkManager.GetLinkList((int)LinkPosition.Navigation, 1));
            //dict.Put(TagFields.GENERAL_LINKS, LinkManager.GetLinkList((int)LinkPosition.General, 1));

            #endregion

            #region 统计

            //dict.Put(TagFields.POST_COUNT, StatisticsManager.GetStatistics().PostCount);
            //dict.Put(TagFields.COMMENT_COUNT, StatisticsManager.GetStatistics().CommentCount);
            //dict.Put(TagFields.VIEW_COUNT, StatisticsManager.GetStatistics().VisitCount);
            //dict.Put(TagFields.AUTHOR_COUNT, UserManager.GetUserList().FindAll(delegate(UserInfo user) { return user.Status == 1; }).Count);

            #endregion
        }

        /// <summary>
        /// 加载文章列表
        /// </summary>
        protected void LoadPostList()
        {
            //int categoryId = -1;
            //int tagId = -1;
            //int userId = -1;
            //string keyword = string.Empty;
            //string data = string.Empty;
            //string begindate = string.Empty;
            //string enddate = string.Empty;

            //int pageindex = RequestHelper.QueryInt("page", 1);

            //string messageinfo = string.Empty;

            //string url = ConfigHelper.SiteUrl + "page/{0}" + SettingService.GetSetting().RewriteExtension;

            //if (pageType == "category")
            //{
            //    string slug = RequestHelper.QueryString("slug");
            //    CategoryInfo cate = CategoryManager.GetCategory(slug);
            //    if (cate != null)
            //    {
            //        categoryId = cate.CategoryId;
            //        th.Put(TagFields.META_KEYWORDS, cate.Name);
            //        th.Put(TagFields.META_DESCRIPTION, cate.Description);
            //        th.Put(TagFields.PAGE_TITLE, cate.Name);
            //        messageinfo = string.Format("<h2 class=\"post-message\">分类:{0}</h2>", cate.Name);
            //        url = ConfigHelper.SiteUrl + "category/" + Server.UrlEncode(slug) + "/page/{0}" + SettingManager.GetSetting().RewriteExtension;
            //    }
            //}
            //else if (pageType == "tag")
            //{
            //    string slug = RequestHelper.QueryString("slug");
            //    TagInfo tag = TagManager.GetTagBySlug(slug);
            //    if (tag != null)
            //    {
            //        tagId = tag.TagId;
            //        th.Put(TagFields.META_KEYWORDS, tag.Name);
            //        th.Put(TagFields.META_DESCRIPTION, tag.Description);
            //        th.Put(TagFields.PAGE_TITLE, tag.Name);
            //        messageinfo = string.Format("<h2 class=\"post-message\">标签:{0}</h2>", tag.Name);
            //        url = ConfigHelper.SiteUrl + "tag/" + Server.UrlEncode(slug) + "/page/{0}" + SettingManager.GetSetting().RewriteExtension;
            //    }
            //}
            //else if (pageType == "author")
            //{
            //    string userName = RequestHelper.QueryString("username");
            //    UserInfo user = UserManager.GetUser(userName);
            //    if (user != null)
            //    {
            //        userId = user.UserId;
            //        th.Put(TagFields.META_KEYWORDS, user.Name);
            //        th.Put(TagFields.META_DESCRIPTION, user.Description);
            //        th.Put(TagFields.PAGE_TITLE, user.Name);
            //        messageinfo = string.Format("<h2 class=\"post-message\">作者:{0}</h2>", user.Name);
            //        url = ConfigHelper.SiteUrl + "author/" + Server.UrlEncode(userName) + "/page/{0}" + SettingManager.GetSetting().RewriteExtension;
            //    }
            //}
            //else if (pageType == "search")
            //{
            //    keyword = StringHelper.CutString(StringHelper.SqlEncode(RequestHelper.QueryString("keyword")), 15);
            //    th.Put(TagFields.META_KEYWORDS, keyword);
            //    th.Put(TagFields.META_DESCRIPTION, keyword);
            //    th.Put(TagFields.PAGE_TITLE, keyword);
            //    th.Put(TagFields.SEARCH_KEYWORD, keyword);
            //    messageinfo = string.Format("<h2 class=\"post-message\">搜索:{0}</h2>", keyword);
            //    url = ConfigHelper.SiteUrl + "search" + SettingManager.GetSetting().RewriteExtension + "?keyword=" + Server.UrlEncode(keyword) + "&page={0}";

            //}
            //else if (pageType == "archive")     //先按月归档
            //{
            //    string year = RequestHelper.QueryString("year");
            //    string month = RequestHelper.QueryString("month");
            //    DateTime date = Convert.ToDateTime(year + "-" + month);
            //    begindate = date.ToString();
            //    enddate = date.AddMonths(1).ToString();
            //    th.Put(TagFields.META_KEYWORDS, "归档");
            //    th.Put(TagFields.META_DESCRIPTION, SettingManager.GetSetting().SiteName + date.ToString("yyyy-MM") + "的归档");
            //    th.Put(TagFields.PAGE_TITLE, "归档:" + date.ToString("yyyy-MM"));
            //    messageinfo = string.Format("<h2 class=\"post-message\">归档:{0}</h2>", date.ToString("yyyy-MM"));
            //    url = ConfigHelper.SiteUrl + "archive/" + date.ToString("yyyyMM") + "/page/{0}" + SettingManager.GetSetting().RewriteExtension;
            //}
            //else    //首页
            //{
            //    if (pageindex == 1)
            //    {
            //        th.Put(TagFields.IS_DEFAULT, "1");
            //    }
            //}

            //th.Put(TagFields.POST_MESSAGE, messageinfo);
            ////     th.Put(TagFields.PAGER_INDEX, pageindex);

            int recordCount = 0;
            dict.Put(TagFields.POSTS, PostsService.GetPostsList(1,100));
        }
    }
}